import { AlbumType } from "../constant/index";
import { useReducer } from "react";
export type InitiaState = {
  search: {
    keyword?: string;
    classify?: number[];
  };
  id: number;
  type: AlbumType | null;
  page: number;
  limit: number;
  dataSource: any[];
  loading: boolean;
};
export enum ActionState {
  SET_SEARCH = "setSearch",
  SET_ID = "setId",
  RESET = "reset",
  SET_TYPE = "setType",
  SET_DATASOURCE = "setDataSource",
  SET_LOADING = "setLoading",
}

export type Action = { type: ActionState; payload?: any };
type Reducer = React.Reducer<InitiaState, Action>;
const reducer: Reducer = (state, action) => {
  switch (action.type) {
    case ActionState.SET_SEARCH: {
      return {
        ...state,
        search: action.payload,
      };
    }

    case ActionState.SET_ID: {
      return {
        ...state,
        id: action.payload,
      };
    }

    case ActionState.RESET: {
      return {
        search: {},
        type: null,
        id: 0,
        page: 1,
        limit: 10,
        dataSource: [],
        loading: false,
      };
    }
    case ActionState.SET_TYPE: {
      return {
        ...state,
        type: action.payload,
      };
    }
    case ActionState.SET_DATASOURCE: {
      return {
        ...state,
        dataSource: action.payload,
      };
    }
    case ActionState.SET_LOADING: {
      return {
        ...state,
        loading: action.payload,
      };
    }
    default: {
      return {
        ...state,
      };
    }
  }
};
export const initState: InitiaState = {
  search: {},
  id: 0,
  type: null,
  page: 1,
  limit: 10,
  dataSource: [],
  loading: true,
};
const usePicStore = () => {
  const [state, dispatch] = useReducer<Reducer>(reducer, initState);
  return {
    state,
    dispatch,
    ActionState,
  };
};

export default usePicStore;
